package user

import (
	"net/http"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

func Logout(c *gin.Context) {
	session := sessions.Default(c)
	user := session.Get("user")
	if user != nil {
		session.Clear()
		err := session.Save()
		if err != nil {
			c.JSON(http.StatusOK, gin.H{"code": 4001, "data": "登出失败"})
			return
		}
	} else {
		c.JSON(http.StatusOK, gin.H{"code": 4001, "data": "你还未登录！"})
		return	
	}

	c.JSON(http.StatusOK, gin.H{"code": 200, "data": "登出成功"})
}